<?php
class Login_Model_Acl extends Zend_Db_Table_Abstract
{
    public function setAcl($user)
    {
        try {

            $this->_db->getConnection();
            
            $query = $this->_db
                          ->select()
                          ->from('access_roles',
                                 array(
                                     'roles_name',
                                 )
                            )
                          ->where(
                                'roles_id' . ' = ?',
                                $user['users_role']
                            );
            $role = $this->_db->fetchRow($query);

            
            $query = $this->_db
                          ->select()
                          ->from('access_rules',
                                 array(
                                     'rules_resource',
                                     'rules_privileg',
                                 )
                            )
                          ->where(
                                'rules_roles_id' . ' = ?',
                                $user['users_role']
                            );
            $resources = $this->_db->fetchAll($query);

            $acl = new Zend_Acl;
            $acl->addRole(new Zend_Acl_Role($role['roles_name']));
            
            foreach ($resources as $resource) {

                $acl->addResource($resource['rules_resource']);
                $privileges = array();
                $privileges = explode(',', $resource['rules_privileg']);
                $acl->allow(
                    $role['roles_name'],
                    $resource['rules_resource'],
                    $privileges
                );
            }
            $aclSession = new Zend_Session_Namespace('Zend_Object');
            $aclSession->acl = $acl;

            return TRUE;
        } catch (Exception $e) {

            return FALSE;
        }
    }
}